home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / error_reporter.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  11.0 KB  |  357 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import os
  5. import sys
  6. import time
  7. import socket
  8. import urllib
  9. import urllib2
  10. import platform
  11. import threading
  12. import wx
  13. import win32api
  14. import pywintypes
  15. if hasattr(sys, 'frozen'):
  16.     import StringIO
  17.     sys.stderr = StringIO.StringIO()
  18.     sys.stdout = StringIO.StringIO()
  19.  
  20. from spamexperts import se_logging
  21.  
  22. try:
  23.     from se_config import spamexpertsConfig
  24. except Exception:
  25.     spamexpertsConfig = None
  26.  
  27.  
  28. try:
  29.     from spamexperts.LSPControl import LSPControl
  30. except Exception:
  31.     LSPControl = None
  32.  
  33.  
  34. try:
  35.     _
  36. except NameError:
  37.     
  38.     _ = lambda x: x
  39.  
  40.  
  41. class ReportData(wx.Dialog):
  42.     
  43.     def __init__(self, parent = None):
  44.         wx.Dialog.__init__(self, parent, title = _('Error Report'))
  45.         self.email_label = wx.StaticText(self, label = _('Your email address:'))
  46.         self.email = wx.TextCtrl(self)
  47.         if spamexpertsConfig:
  48.             self.email.SetValue(spamexpertsConfig.user_address)
  49.         
  50.         label = _('Problem description:')
  51.         self.problem_label = wx.StaticText(self, label = label)
  52.         self.problem = wx.TextCtrl(self, size = (-1, 200), style = wx.TE_MULTILINE)
  53.         self.ok = wx.Button(self, wx.ID_OK)
  54.         self.cancel = wx.Button(self, wx.ID_CANCEL)
  55.         sizer = wx.GridBagSizer(5, 5)
  56.         sizer.Add(self.email_label, (0, 0), (1, 1), wx.ALIGN_LEFT | wx.ALL, 5)
  57.         sizer.Add(self.email, (0, 1), (1, 2), wx.EXPAND | wx.ALL, 5)
  58.         sizer.Add(self.problem_label, (1, 0), (1, 1), wx.ALIGN_CENTER | wx.ALL, 5)
  59.         sizer.Add(self.problem, (2, 0), (1, 3), wx.EXPAND | wx.ALL, 5)
  60.         sizer.Add(self.ok, (3, 2), (1, 1), wx.ALIGN_CENTER | wx.ALL, 5)
  61.         sizer.Add(self.cancel, (3, 1), (1, 1), wx.ALIGN_CENTER | wx.ALL, 5)
  62.         self.SetSizerAndFit(sizer)
  63.         self.GetProblem = self.problem.GetValue
  64.         self.GetEmail = self.email.GetValue
  65.  
  66.  
  67.  
  68. class ErrorReporter(object):
  69.     
  70.     def __init__(self):
  71.         self.taskManager = None
  72.  
  73.     
  74.     def getOSVersion(self):
  75.         sysPlatform = platform.system()
  76.         
  77.         try:
  78.             winVersions = {
  79.                 (1, 4, 0): '95',
  80.                 (1, 4, 10): '98',
  81.                 (1, 4, 90): 'ME',
  82.                 (2, 4, 0): 'NT',
  83.                 (2, 5, 0): '2K',
  84.                 (2, 5, 1): 'XP' }
  85.             winOSdetails = sys.getwindowsversion()
  86.             a = winOSdetails
  87.             winVersion = winVersions[(a[3], a[0], a[1])]
  88.             osVersion = '%s %s (%s.%s.%s %s)' % (sysPlatform, winVersion, a[0], a[1], a[2], a[4])
  89.         except Exception:
  90.             osVersion = 'Non-Windows OS (%s)' % (sysPlatform,)
  91.  
  92.         return osVersion
  93.  
  94.     
  95.     def getTaskManager(self):
  96.         """Returns all data provided by Windows' Task Manager."""
  97.         
  98.         try:
  99.             import win32pdh
  100.             import win32pdhutil
  101.         except ImportError:
  102.             print 'No performance data helper installed'
  103.             self.getting_tasks = 0
  104.             return None
  105.  
  106.         
  107.         try:
  108.             (processinfo, processes) = win32pdh.EnumObjectItems(None, None, 'Process', -1)
  109.         except pywintypes.error:
  110.             self.getting_tasks = 0
  111.             return { }
  112.         except RuntimeError:
  113.             self.getting_tasks = 0
  114.             return { }
  115.  
  116.         self.taskManager = { }
  117.         find_by_name = win32pdhutil.FindPerformanceAttributesByName
  118.         for process in processes:
  119.             for info in processinfo:
  120.                 
  121.                 try:
  122.                     details = find_by_name(process, counter = info)
  123.                     
  124.                     try:
  125.                         self.taskManager[process][info] = details
  126.                     except KeyError:
  127.                         self.taskManager[process] = {
  128.                             info: details }
  129.  
  130.                 continue
  131.                 except pywintypes.error:
  132.                     continue
  133.                     continue
  134.                 
  135.  
  136.             
  137.         
  138.         self.getting_tasks = 0
  139.         return self.taskManager
  140.  
  141.     
  142.     def getLspInfo(self):
  143.         if LSPControl:
  144.             lspControl = LSPControl()
  145.             return lspControl.getLSPInfo()
  146.         else:
  147.             return 'Could not import LSPControl'
  148.  
  149.     
  150.     def generateReport(self, callback = None, email = None, problem = None):
  151.         if callback:
  152.             callback(5, _('Getting IP'))
  153.         
  154.         old_timeout = socket.getdefaulttimeout()
  155.         socket.setdefaulttimeout(3)
  156.         
  157.         try:
  158.             local_ip = socket.gethostbyname(socket.gethostname())
  159.         except socket.timeout:
  160.             local_ip = '{unknown}'
  161.  
  162.         socket.setdefaulttimeout(old_timeout)
  163.         yield ('Report from', local_ip)
  164.         if email:
  165.             yield ('User email address', email)
  166.         elif spamexpertsConfig:
  167.             address = spamexpertsConfig.user_address
  168.             yield ('User email address', address)
  169.         
  170.         if problem:
  171.             yield ('Problem description', problem)
  172.         
  173.         if callback:
  174.             callback(10, _('Getting OS'))
  175.         
  176.         yield ('OS details', self.getOSVersion())
  177.         if callback:
  178.             callback(15, _('Getting process list'))
  179.         
  180.         self.getting_tasks = time.time()
  181.         get_tasks = threading.Thread(target = self.getTaskManager)
  182.         get_tasks.start()
  183.         while self.getting_tasks:
  184.             if time.time() > self.getting_tasks + 60 * 3:
  185.                 self.taskManager = {
  186.                     'Timed out': None }
  187.                 break
  188.             
  189.             time.sleep(1)
  190.         if callback:
  191.             callback(20, _('Getting LSPs'))
  192.         
  193.         lspInfo = self.getLspInfo()
  194.         yield ([], []([ lspInfoLine for lspInfoLine in lspInfo.splitlines() ]))
  195.         lsp_log_fn = os.path.join(win32api.GetSystemDirectory(), 'SpamExpertsLSP.txt')
  196.         if os.path.exists(lsp_log_fn):
  197.             log_data = file(lsp_log_fn).read()
  198.             if len(log_data) > 1024 * 1024:
  199.                 yield ('LSP Log', 'LSP log file is too large to include.')
  200.             else:
  201.                 yield ('LSP Log', log_data)
  202.         
  203.         if callback:
  204.             callback(30, _('Getting IDs'))
  205.         
  206.         if spamexpertsConfig:
  207.             yield ('Fingerprint Database Unique ID', spamexpertsConfig.user_id)
  208.         else:
  209.             yield ('Could not get configuration.', '')
  210.         for i in xrange(1, 5):
  211.             if callback:
  212.                 callback(30 + 2 * i, _('Getting log'))
  213.             
  214.             log_fn = os.path.join(se_logging.log_dir, 'SpamExperts%s.log' % (i,))
  215.             if os.path.exists(log_fn):
  216.                 log_data = file(log_fn).read()
  217.                 if len(log_data) > 1024 * 1024:
  218.                     yield ('Log file %s' % (i,), 'Log file is too large to include')
  219.                 else:
  220.                     yield ('Log file %s' % (i,), log_data)
  221.             len(log_data) > 1024 * 1024
  222.         
  223.         if callback:
  224.             callback(55, _('Getting POP log'))
  225.         
  226.         pop_log_fn = os.path.join(se_logging.log_dir, '_pop3proxy.log')
  227.         if os.path.exists(pop_log_fn):
  228.             log_data = file(pop_log_fn).read()
  229.             if len(log_data) > 1024 * 1024:
  230.                 yield ('POP3 Proxy Log', 'Log file is too large to include')
  231.             else:
  232.                 yield ('POP3 Proxy Log', log_data)
  233.         
  234.         if callback:
  235.             callback(60, _('Getting IMAP log'))
  236.         
  237.         imap_log_fn = os.path.join(se_logging.log_dir, '_imap4proxy.log')
  238.         if os.path.exists(imap_log_fn):
  239.             log_data = file(imap_log_fn).read()
  240.             if len(log_data) > 1024 * 1024:
  241.                 yield ('IMAP4 Proxy Log', 'Log file is too large to include')
  242.             else:
  243.                 yield ('IMAP4 Proxy Log', log_data)
  244.         
  245.         if callback:
  246.             callback(65, _('Getting copyfile log'))
  247.         
  248.         copyfile_log_fn = os.path.join(se_logging.log_dir, 'se_copyfile.log')
  249.         if os.path.exists(copyfile_log_fn):
  250.             log_data = file(copyfile_log_fn).read()
  251.             if len(log_data) > 1024 * 1024:
  252.                 yield ('copyfile log', 'Log file is too large to include')
  253.             else:
  254.                 yield ('copyfile log', log_data)
  255.         
  256.         yield self.get_stats(callback)
  257.  
  258.     
  259.     def get_stats(callback):
  260.         if callback:
  261.             callback(70, _('Getting performance statistics'))
  262.         
  263.         
  264.         try:
  265.             options = options
  266.             import spamexperts.Options
  267.         except Exception:
  268.             return ('Performance Statistics', "Couldn't import options")
  269.  
  270.         
  271.         try:
  272.             message = message
  273.             import spambayes
  274.             (fn, typ) = message.database_type()
  275.             db = message.open_storage(fn, typ, 'r')
  276.         except Exception:
  277.             
  278.             try:
  279.                 db.close()
  280.             except:
  281.                 pass
  282.  
  283.             return ('Performance Statistics', "Couldn't get message db")
  284.  
  285.         
  286.         try:
  287.             message = message
  288.             import spamexperts
  289.             m_class = message.SEHeaderMessage
  290.         except Exception:
  291.             db.close()
  292.             return ('Performance Statistics', "Couldn't get message class")
  293.  
  294.         
  295.         try:
  296.             se_stats = se_stats
  297.             import spamexperts
  298.         except Exception:
  299.             db.close()
  300.             return ('Performance Statistics', "Couldn't import stats")
  301.  
  302.         statistics = se_stats.SEStats(options, db, m_class)
  303.         stats = []([ a for a, unused in statistics.GetStats() ])
  304.         db.close()
  305.         return ('Performance Statistics', stats)
  306.  
  307.     get_stats = staticmethod(get_stats)
  308.     base_url = 'http://spamexperts.com/cgi-bin/error_receiver.py'
  309.     
  310.     def submit_report(self, callback, email = None, problem = None):
  311.         if email is None or problem is None:
  312.             dlg = ReportData()
  313.             result = dlg.ShowModal()
  314.             if result == wx.ID_CANCEL:
  315.                 print 'Cancelled report'
  316.                 return (True, 'Cancelled')
  317.             
  318.             email = dlg.GetEmail()
  319.             problem = dlg.GetProblem()
  320.             dlg.Destroy()
  321.         
  322.         report = self.generateReport(callback, email, problem)
  323.         for encoding in ('ascii', 'latin-1', 'koi8-r'):
  324.             
  325.             try:
  326.                 report = []([ (title.encode(encoding), data.encode(encoding)) for title, data in report ])
  327.             except UnicodeEncodeError:
  328.                 continue
  329.  
  330.         else:
  331.             print >>sys.stderr, 'Could not encode report.  Cancelling.'
  332.             return (False, "Couldn't encode")
  333.         callback(75, _('Transmitting'))
  334.         
  335.         try:
  336.             url = urllib2.urlopen(self.base_url, urllib.urlencode(report))
  337.         except (urllib2.HTTPError, urllib2.URLError, socket.error):
  338.             e = None
  339.             print >>sys.stderr, 'Could not submit report', str(e)
  340.             return (False, str(e))
  341.  
  342.         callback(99, _('Complete'))
  343.         print 'Sent report (%s)' % (url.read(),)
  344.         return (True, 'Complete')
  345.  
  346.  
  347. if __name__ == '__main__':
  348.     e = ErrorReporter()
  349.     
  350.     callback = lambda x, y: pass
  351.     if spamexpertsConfig:
  352.         email = spamexpertsConfig.user_address
  353.     else:
  354.         email = 'unknown@example.com'
  355.     e.submit_report(callback, email, 'copyfile.exe failed.')
  356.  
  357.